package linker.buddy;

import java.util.Collection;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;

import linker.account.Account;
import linker.chat.ChatCell;
import linker.list.Model;
import linker.list.SubscribeRequest;

/**
 * 
 * @version linker 0.93
 * @author Jianfeng coxzjkwl@gmail.com
 * 
 */
public class BuddyList extends Model {
	private static final long serialVersionUID = 1L;

	private Vector<ChatCell> list = new Vector<ChatCell>();

	private XMPPConnection conn = Account.getConnection();

	private Roster roster = Account.getRoster();

	private String last = "";

	public void addBuddy(RosterEntry entry) {
		ChatCell chatCell = new ChatCell(entry);
		list.addElement(chatCell);

		refilter(null);
	}

	public void removeBuddy(String user) {
		for (int i = 0; i < list.size(); i++) {

			if (list.elementAt(i).getUser().indexOf(user) >= 0) {
				list.removeElementAt(i);
				refilter(null);
				return;
			}
		}
	}

	public void addElement(String user) {
		int i = JOptionPane.showConfirmDialog(null, "No such user, add " + user
				+ " ?", "Subscribe Request To " + user,
				JOptionPane.YES_NO_OPTION);

		if (i == JOptionPane.OK_OPTION) {
			Presence presence = new Presence(Presence.Type.subscribe);
			presence.setFrom(conn.getUser());
			presence.setTo(user);
			conn.sendPacket(presence);
		}
	}

	public void changeByPresence(Presence presence) {
		int i;

		if (presence.getType() == Presence.Type.subscribe) {
			Account.getMessageList().addMessage(
					new SubscribeRequest(presence.getFrom()));

			return;
		} else if (presence.getType() == Presence.Type.subscribed) {
			String user = presence.getFrom();
			try {
				roster.createEntry(user, user, null);
			} catch (XMPPException e) {
				e.printStackTrace();
			}

			addBuddy(roster.getEntry(user));
		} else if (presence.getType() == Presence.Type.unsubscribed) {
			String user = presence.getFrom();
			try {
				removeBuddy(user);
				roster.removeEntry(roster.getEntry(user));
			} catch (XMPPException e) {
				e.printStackTrace();
			}
		}

		for (i = 0; i < list.size(); i++) {
			if (presence.getFrom().indexOf(
					list.elementAt(i).getEntry().getUser()) >= 0) {

				ChatCell chatCell = list.elementAt(i);
				chatCell.setPresence(presence);
				removeElement(chatCell);
				add(i, chatCell);

				return;
			}
		}

	}

	public void refilter(String s) {

		if (s != null)
			last = s;
		else
			s = last;

		ChatCell chatCell;
		clear();

		for (int i = 0; i < list.size(); i++) {
			chatCell = list.elementAt(i);

			if (chatCell.toString().indexOf(s) >= 0)
				addElement(chatCell);
		}
	}

	public void fill(Roster roster) {
		Collection<RosterEntry> rosterEntries = roster.getEntries();

		for (RosterEntry rosterEntry : rosterEntries)
			addBuddy(rosterEntry);
	}

	public void processMessage(Message message) {
		for (int i = 0; i < list.size(); i++) {
			ChatCell chatCell = list.elementAt(i);

			if (message.getFrom().indexOf(chatCell.getEntry().getUser()) >= 0)
				chatCell.setMessage(message);
		}
	}
}
